<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover">
      <div class="system-menu-search mb15">
        <el-input v-model="state.keywords" size="default" placeholder="请输入文章标题"
                  style="max-width: 180px"></el-input>
        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon>
            <Search/>
          </el-icon>
          搜索
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="handleAdd">
          <el-icon>
            <Plus/>
          </el-icon>
          新增文章
        </el-button>
      </div>
      <el-table :data="state.tableData.data" stripe style="width: 100%">
        <el-table-column label="标题" prop="title" min-width="200px"/>

        <el-table-column label="关键字" prop="keywords" min-width="125px">

        </el-table-column>

        <el-table-column label="标签" prop="tag" min-width="100px">
          <template #header>
            <el-select
                v-model="state.mytag"
                @change="getTableData()"
                size="small"
                style="width: 100px"
            >
              <el-option
                  v-for="item in state.tags"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value"
              />
              <el-option
                  key="0"
                  label="全部文章"
                  :value="0"
              />
            </el-select>
          </template>
          <template #default="scope">
            <el-tag type="primary" disable-transitions>{{ scope.row.articleType.name }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="作者" prop="author" min-width="140px">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-avatar style="width: 40px; height: 40px" :src="scope.row.userInfo.avatar" fit="cover"/>
              <span style="margin-left: 10px">{{ scope.row.userInfo.user.name }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="发布时间" prop="createdAt" min-width="125px"/>


        <el-table-column label="是否展示" prop="top">
          <template #header>
            <el-select
                style="width: 100px"
                @change="getTableData()"
                class="m-2" size="small" v-model="state.tops">
              <el-option label='轮播' :value="1" :key="1"/>
              <el-option label='不轮播' :value="0" :key="0"/>
              <el-option label='全部' :value="-1" :key="-1"/>
            </el-select>
          </template>
          <template #default="scope">
            <el-switch
                v-model="scope.row.tops"
                @change="handletop(scope.row)"
                inline-prompt
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"/>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="onRowDel(scope.row)">删除</el-button>
            <el-link v-if="scope.row.articlelink" :href="scope.row.articlelink" target="_blank">
              <el-icon class="mr5">
                <View/>
              </el-icon>
              链接
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          size="small"
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import {defineAsyncComponent, onMounted, reactive, ref} from "vue";
import {ElMessageBox} from "element-plus";
import {Plus, Search, View} from "@element-plus/icons-vue";
import {deleteArticle, getArticleInfo, istop} from "/@/views/article/article";
import Axios from "/@/utils/axios.ts";
import router from "/@/router";
// 引入组件

const handleEdit = (index: number, row: any) => {
  router.push({path: '/editarticle', query: {id: row.id, tagsViewName: '文章' + row.id, type: "edit"}});
};
const handleAdd = () => {
  router.push({path: '/addarticle'});
};
const handletop = (row: any) => {
  console.log(row);
  istop({
        checked: row.tops,
        id: row.id
      }
  );
};
// 定义变量内容
const state = reactive({
  tops: -1,
  tags: [],
  users: [],
  keywords: "",
  mytag: 0,
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10
    }
  }
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  getArticleInfo(
      {
        page: state.tableData.param.pageNum,
        pageSize: state.tableData.param.pageSize,
        keywords: state.keywords,
        mytag: state.mytag,
        tops: state.tops
      }
  ).then((res) => {
    state.tableData.data = res.data.articleList;
    state.tags = res.data.tag.map((item: any) => {
      return {
        value: item.id,
        text: item.name
      };
    });
    state.tags.push();
    state.tableData.total = res.data.total;

  });
  setTimeout(() => {
    state.tableData.loading = false;
  }, 500);
};
// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {

};

// 删除用户
const onRowDel = (row: any) => {
  ElMessageBox.confirm(`此操作将永久删除"${row.title}"，是否继续?`, "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  })
      .then(() => {
        deleteArticle(row.id).then((res) => {
          getTableData();
        });
      })
      .catch(() => {
      });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  Axios.get("/backapi/users").then((res: any) => {
    state.users = res.data.allUser;
  });

  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  padding: 0px;

  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
